package com.fw.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fw.entity.MyException;
import com.fw.entity.Pig;
import com.fw.entity.Result;
import com.fw.enums.PigCheckState;
import com.fw.enums.PigState;
import com.fw.service.IPigService;
import com.fw.utils.JWTUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/check")
@RestController
public class CheckController {
    @Resource
    IPigService pigService;
    @RequestMapping("/passAllPig")
    public Result passAllPig() throws MyException {
        Map<String, Object> m = new HashMap<>();
        List<Pig> pigList = pigService.list(new QueryWrapper<Pig>()
                .eq("pigraisecheck", PigCheckState.CHECKING.getValue())
                .or()
                .eq("pigkillcheck", PigCheckState.CHECKING.getValue()));
        for (Pig pig :
                pigList) {
            if(pig.getPigraisecheck().equals(PigCheckState.CHECKING.getValue())){
                pig.setPigraisecheck(PigCheckState.PASS.getValue());
            }else if(pig.getPigkillcheck().equals(PigCheckState.CHECKING.getValue())){
                pig.setPigkillcheck(PigCheckState.PASS.getValue());
            }
        }
        pigService.saveOrUpdateBatch(pigList);
        return new Result(null, "通过全部待检疫生猪完成", "000000");
    }

    @RequestMapping("/passSomePig")
    public Result passSomePig(@RequestParam String pigids) throws MyException {
        if(pigids.equals(""))
            throw new MyException("输入不能为空", "060102");
        JSONArray numarray = JSONUtil.parseArray(pigids);
        List<Integer> ids = numarray.toList(Integer.TYPE);
        List<Pig> pigList = pigService.listByIds(ids);
        for (Pig pig :
                pigList) {
            if (pig.getPigraisecheck().equals(PigCheckState.CHECKING.getValue())) {
                pig.setPigraisecheck(PigCheckState.PASS.getValue());
            } else if (pig.getPigkillcheck().equals(PigCheckState.CHECKING.getValue())) {
                pig.setPigkillcheck(PigCheckState.PASS.getValue());
            }else {
                throw new MyException("存在检疫状态不是待检疫的生猪，请检查选择的生猪", "060102");
            }
        }
        pigService.saveOrUpdateBatch(pigList);
        return new Result(null, "通过选定待检疫生猪完成", "000000");
    }

    @RequestMapping("/passOnePig")
    public Result passOnePig(@RequestParam String pigids) throws MyException {
        if(pigids.equals(""))
            throw new MyException("输入不能为空", "060201");
        Map<String, Object> m = new HashMap<>();
        m.put("pigid", pigids);
        List<Pig> pigList = pigService.listByMap(m);
        if(pigList.isEmpty())
            throw new MyException("该生猪不存在", "060202");
        Pig pig = pigList.get(0);
        if (pig.getPigraisecheck().equals(PigCheckState.CHECKING.getValue())) {
            pig.setPigraisecheck(PigCheckState.PASS.getValue());
        } else if (pig.getPigkillcheck().equals(PigCheckState.CHECKING.getValue())) {
            pig.setPigkillcheck(PigCheckState.PASS.getValue());
        }else {
            throw new MyException("该生猪状态不是检疫中", "060101");
        }
        pigService.saveOrUpdate(pig);
        return new Result(null, "通过某个待检疫生猪完成", "000000");
    }
}
